package jp.co.iris_ltd.irisphotoeditor.util;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Join;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.view.MotionEvent;

public class PenArtTool extends ArtTool {
    public static final int INLINE_DEFAULT_COLOR = Color.BLACK;
    public static final boolean INLINE_DEFAULT_ANTI_ALIAS = true;
    public static final int INLINE_DEFAULT_STROKE_WIDTH = 5;
    public static final Paint.Style INLINE_DEFAULT_STYLE = Style.STROKE;
    public static final Cap INLINE_DEFAULT_CAP = Cap.ROUND;
    public static final Join INLINE_DEFAULT_JOIN = Join.ROUND;

    public static final int OUTLINE_DEFAULT_COLOR = Color.LTGRAY;
    public static final boolean OUTLINE_DEFAULT_ANTI_ALIAS = true;
    public static final int OUTLINE_DEFAULT_STROKE_WIDTH = 10;
    public static final Paint.Style OUTLINE_DEFAULT_STYLE = Style.STROKE;
    public static final Cap OUTLINE_DEFAULT_CAP = Cap.ROUND;
    public static final Join OUTLINE_DEFAULT_JOIN = Join.ROUND;

    private float mInlineStrokeWidth = INLINE_DEFAULT_STROKE_WIDTH;
    private float mOutlineStrokeWidth = OUTLINE_DEFAULT_STROKE_WIDTH;

    private Path mPath = new Path();
    private Paint mOutlinePaint = new Paint();

    public PenArtTool() {
        init();
    }

    public PenArtTool(Paint inlinePaint, Paint outlinePaint) {
        mMode = ToolMode.Pen;

        mPaint = new Paint(inlinePaint);
        mOutlinePaint = new Paint(outlinePaint);
    }

    protected void init() {
        mMode = ToolMode.Pen;

        mOutlinePaint.setColor(OUTLINE_DEFAULT_COLOR);
        mOutlinePaint.setStyle(OUTLINE_DEFAULT_STYLE);
        mOutlinePaint.setAntiAlias(OUTLINE_DEFAULT_ANTI_ALIAS);
        mOutlinePaint.setStrokeWidth(mOutlineStrokeWidth);
        mOutlinePaint.setStrokeCap(OUTLINE_DEFAULT_CAP);
        mOutlinePaint.setStrokeJoin(OUTLINE_DEFAULT_JOIN);

        mPaint.setColor(INLINE_DEFAULT_COLOR);
        mPaint.setStyle(INLINE_DEFAULT_STYLE);
        mPaint.setAntiAlias(INLINE_DEFAULT_ANTI_ALIAS);
        mPaint.setStrokeWidth(mInlineStrokeWidth);
        mPaint.setStrokeCap(INLINE_DEFAULT_CAP);
        mPaint.setStrokeJoin(INLINE_DEFAULT_JOIN);
    }

    @Override
    public void setPosition(MotionEvent event, float scale) {
        float x = ((event.getX() / scale) - mOffsetX);
        float y = ((event.getY() / scale) - mOffsetY);
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mPath.reset();
            mPath.moveTo(x, y);
            mX = x;
            mY = y;
            break;
        case MotionEvent.ACTION_MOVE:
           // mPath.quadTo(mX, mY, x, y);
           mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;
            break;
        case MotionEvent.ACTION_UP:
            if (x == mX && y == mY) {
                mY = mY + 1;
            }
            mPath.quadTo(mX, mY, x, y);
            break;
        default:
            break;
        }
    }

    @Override
    public void drawCanvas(Canvas canvas) {
        if (!mIsFinalized) {
            canvas.drawPath(mPath, mOutlinePaint);
            canvas.drawPath(mPath, mPaint);
        } else {
            mPath.offset(mOffsetX, mOffsetY);
            canvas.drawPath(mPath, mOutlinePaint);
            canvas.drawPath(mPath, mPaint);
        }
    }

    @Override
    public void restoreCanvas(Canvas canvas) {
    }

    @Override
    public void onDrawingFinish() {
        mPath.reset();
    }

    public void setOutlinePaint(Paint paint) {
        mOutlinePaint = paint;
    }

    public Paint getOutlinePaint() {
        return mOutlinePaint;
    }
}
